package CGW.service;

import CGW.bean.Dinner_table;
import CGW.bean.Page;
import CGW.dao.Dinner_tableDao;

import java.util.List;

public class Dinner_tableService {
    private Dinner_tableDao dao;

    public Dinner_tableService(Dinner_tableDao dao) {
        this.dao = dao;
    }

    public List<Dinner_table> queryAll(){
        List<Dinner_table> ret = dao.all(1, 100);
        dao.close();
        return ret;
    }

    public Page<Dinner_table> queryAll(int page, int pageSize){
        int total = dao.total();
        List<Dinner_table> ret = dao.all(page, pageSize);
        Page<Dinner_table> pageBean = new Page<>(ret, page, (int)Math.ceil(total*1.0/pageSize), pageSize);
        dao.close();
        return pageBean;
    }

    public Dinner_table get(int table_id){
        Dinner_table u = dao.get(table_id);
        dao.close();
        return u;
    }

    public boolean insert(Dinner_table dinner_table){
        int rows = dao.add(dinner_table);
        dao.commit();
        dao.close();
        return rows > 0;
    }

    public boolean update(Dinner_table food){
        int rows = dao.update(food);
        dao.commit();
        dao.close();
        return  rows > 0;
    }
    public boolean delete(int table_id){
        int rows = dao.delete(table_id);
        dao.commit();
        dao.close();
        return  rows > 0;
    }


    public void close(){
        dao.close();
    }
}
